import React, { Component } from "react";
import "./index.less";
import { base } from "nc-lightapp-front";
import getPostInfo from "../../request-functions/getPostInfo";
import { HI_PSNJOB_H_DEFS } from "../../request-functions/constant";
import language from "../../language";

const { NCModal, NCButton, NCCheckbox, NCTextArea } = base;
const { NCRow, NCCol, NCFormControl, NCForm } = base;

const NCFormItem = NCForm.NCFormItem;
const {
    Header: NCModalHeader,
    Body: NCModalBody,
    Footer: NCModalFooter,
} = NCModal;

/**
 * 职业危害调研模态框
 */
class ZywhdyModal extends Component {
    constructor(props) {
        super(props);

        this.showHandle = this.showHandle.bind(this);
        this.closeInfoModal = this.closeInfoModal.bind(this);
        this.verifyOnlyInfo = this.verifyOnlyInfo.bind(this);
        this.zywhdyGetPostInfoData = this.zywhdyGetPostInfoData.bind(this);

        this.state = {
            zywhdyData: this.props.zywhdyData, // 获取到职业危害调研的数据
        };
    }

    verifyOnlyInfo() {
        this.props.updateState({
            zywhdyData: this.state.zywhdyData
        });
        this.closeInfoModal();
        return;
    }

    closeInfoModal() {
        this.props.updateState({
            zywhdyModalVisible: false,
        });
    }

    async zywhdyGetPostInfoData(data) {
        let res = await getPostInfo(data);
        return res;
    }

    async showHandle() {
        const formLists = ['bd_psndoc', 'hi_psnjob_h', 'hi_psnorg_h'];
        let psndocFormVal = this.props.form.getAllFormValue(formLists[0]);
        let psnjobFormVal = this.props.form.getAllFormValue(formLists[1]);

        // 组织
        let pk_org_name = psnjobFormVal.rows[0].values['pk_org'].display;
        // 部门
        let pk_dept_name = psnjobFormVal.rows[0].values['pk_dept'].display;
        // 岗位
        let pk_post_name = psnjobFormVal.rows[0].values['pk_post'].display;
        let pk_post = psnjobFormVal.rows[0].values['pk_post'].value;

        let job_info = `${pk_org_name} ${pk_dept_name} ${pk_post_name}`;

        let resp = await getPostInfo(pk_post);

        let glbdef2,glbdef3,glbdef4,glbdef5 ;
        if (resp && resp.success) {
            glbdef2 = resp.data.glbdef2;
            glbdef3 = resp.data.glbdef3;
            glbdef4 = resp.data.glbdef4;
            glbdef5 = resp.data.glbdef5;
        }
        
        let zywhdyData = this.state.zywhdyData;

        zywhdyData['szbmjgw'] = job_info;
        zywhdyData['qtgwzy'] = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.QTYDYHGWGZ].value;
        zywhdyData['qtyxjb'] = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.QTYXJB].value;

        zywhdyData['zybwhys'] = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.ZYBWHYS].value;
        zywhdyData['zyjjz'] = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.ZYJJZ].value;
        zywhdyData['kndzdzybwh'] = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.KNDZDZYBWH].value;
        zywhdyData['zybfhcs'] = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.ZYBFHCS].value;

        if (!zywhdyData['zybwhys']) {
            zywhdyData['zybwhys'] = glbdef2;
        }
        if (!zywhdyData['zyjjz']) {
            zywhdyData['zyjjz'] = glbdef3;
        }
        if (!zywhdyData['kndzdzybwh']) {
            zywhdyData['kndzdzybwh'] = glbdef4;
        }
        if (!zywhdyData['zybfhcs']) {
            zywhdyData['zybfhcs'] = glbdef5;
        }

        let gwJobFormObj = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.CSGYDYHGWGZ].value;
        let yxDiseaseFormObj = psnjobFormVal.rows[0].values[HI_PSNJOB_H_DEFS.SFYYXJB].value;

        if (gwJobFormObj) {
            let items = gwJobFormObj.split(';');
            for (let i = 0; i < items.length; i++) {
                if (items[i]) {
                    let keyAndValue = items[i].split(':');
                    if (keyAndValue.length == 2) {
                        let val = keyAndValue[1] === 'true' ? true : false;
                        zywhdyData[keyAndValue[0]] = val;
                    }
                }
            }
        }
        if (yxDiseaseFormObj) {
            let items = yxDiseaseFormObj.split(';');
            for (let i = 0; i < items.length; i++) {
                if (items[i]) {
                    let keyAndValue = items[i].split(':');
                    if (keyAndValue.length == 2) {
                        let val = keyAndValue[1] === 'true' ? true : false;
                        zywhdyData[keyAndValue[0]] = val;
                    }
                }
            }
        }

        this.setState({
            zywhdyData:zywhdyData
        });

    }

    onchange = (key, val) => {
        let newZywhdyData = this.state.zywhdyData;
        newZywhdyData[key] = val;

        this.setState({ zywhdyData: newZywhdyData });
    };

    render() {
        const { zywhdyModalVisible } = this.props;
        let zywhdyData = this.state.zywhdyData;
        return (
            <NCModal
                show={zywhdyModalVisible}
                width="900px"
                minHeight="500px"
                className="zywhdy-info-modal"
                onHide={this.closeInfoModal}
                onEntered={this.showHandle}
                backdrop="static"
            >
                <NCModalHeader closeButton={true}>
                    <span>职业危害调研</span>
                </NCModalHeader>
                <NCModalBody>
                    <div className="high-danger-content">
                        <div className="high-danger-item">
                            <span className="high-danger-item-title">
                                一、您是否曾经从事过有毒有害高危工种？如：
                            </span>
                            <div>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("hxlzy", val);
                                    }}
                                    checked={zywhdyData.hxlzy}
                                >
                                    化学类作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("gwzy", val);
                                    }}
                                    checked={zywhdyData.gwzy}
                                >
                                    高温作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("gkzy", val);
                                    }}
                                    checked={zywhdyData.gkzy}
                                >
                                    高空作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("gyzy", val);
                                    }}
                                    checked={zywhdyData.gyzy}
                                >
                                    高压作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("ylrqzy", val);
                                    }}
                                    checked={zywhdyData.ylrqzy}
                                >
                                    压力容器作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("zlzy", val);
                                    }}
                                    checked={zywhdyData.zlzy}
                                >
                                    制冷作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("zszy", val);
                                    }}
                                    checked={zywhdyData.zszy}
                                >
                                    噪声作业
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("fczy", val);
                                    }}
                                    checked={zywhdyData.fczy}
                                >
                                    粉尘作业
                                </NCCheckbox>
                            </div>
                            <div>
                                <span>
                                    或从事过其他有毒有害高危工种，请填写具体工种：
                                </span>
                                <NCFormControl
                                    value={zywhdyData.qtgwzy}
                                    onChange={(val) => {
                                        this.onchange("qtgwzy", val);
                                    }}
                                ></NCFormControl>
                            </div>
                        </div>

                        <div className="high-danger-item">
                            <span className="high-danger-item-title">
                                二、您是否有隐性疾病？如：
                            </span>
                            <div>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("xtxxzb", val);
                                    }}
                                    checked={zywhdyData.xtxxzb}
                                >
                                    先天性心脏病
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("dx", val);
                                    }}
                                    checked={zywhdyData.dx}
                                >
                                    癫痫
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("gxy", val);
                                    }}
                                    checked={zywhdyData.gxy}
                                >
                                    高血压
                                </NCCheckbox>
                                <NCCheckbox
                                    className="wps-checkbox"
                                    color="primary"
                                    onChange={(val) => {
                                        this.onchange("tnb", val);
                                    }}
                                    checked={zywhdyData.tnb}
                                >
                                    糖尿病
                                </NCCheckbox>
                            </div>
                            <div>
                                <span>或其他隐性疾病，请填写具体疾病：</span>
                                <NCFormControl
                                    value={zywhdyData.qtyxjb}
                                    onChange={(val) => {
                                        this.onchange("qtyxjb", val);
                                    }}
                                ></NCFormControl>
                            </div>
                        </div>

                        <div className="high-danger-item">
                            <span className="high-danger-item-title">
                                三、所在工作岗位、可能产生的职业病危害、后果及职业病防护措施：
                            </span>
                            <div>所在部门及岗位：{zywhdyData.szbmjgw}</div>
                            <NCFormItem
                                labelName="职业病危害因素："
                                method="blur"
                            >
                                <NCTextArea
                                    className=""
                                    maxlength={1024}
                                    onChange={(val) => {
                                        this.onchange("zybwhys", val);
                                    }}
                                    value={zywhdyData.zybwhys}
                                />
                            </NCFormItem>
                            <NCFormItem labelName="职业禁忌症：" method="blur">
                                <NCTextArea
                                    className=""
                                    maxlength={1024}
                                    onChange={(val) => {
                                        this.onchange("zyjjz", val);
                                    }}
                                    value={zywhdyData.zyjjz}
                                />
                            </NCFormItem>
                            <NCFormItem
                                labelName="可能导致的职业病危害："
                                method="blur"
                            >
                                <NCTextArea
                                    className=""
                                    maxlength={1024}
                                    onChange={(val) => {
                                        this.onchange("kndzdzybwh", val);
                                    }}
                                    value={zywhdyData.kndzdzybwh}
                                />
                            </NCFormItem>
                            <NCFormItem
                                labelName="职业病防护措施："
                                method="blur"
                            >
                                <NCTextArea
                                    className=""
                                    maxlength={1024}
                                    onChange={(val) => {
                                        this.onchange("zybfhcs", val);
                                    }}
                                    value={zywhdyData.zybfhcs}
                                />
                            </NCFormItem>
                        </div>
                    </div>
                </NCModalBody>
                <NCModalFooter>
                    <NCButton colors="primary" onClick={this.verifyOnlyInfo}>
                        {language["hi6007-000174"]}
                        {/* 国际化处理： 确认*/}
                    </NCButton>
                    <NCButton colors="secondary" onClick={this.closeInfoModal}>
                        {language["hi6007-000005"]}
                        {/* 国际化处理： 取消*/}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default ZywhdyModal;
